layui.config({
    base: ctx + '/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index',         //主入口模块
    treetable: 'treetable'
}).use(['index', 'table', 'treetable'], function () {
    var prefix = ctx + "/manage/menu/menu";
    var $ = layui.$, table = layui.table, treetable = layui.treetable;

    //事件
    var active = {
        batchdel: function () {
            // 遍历所有选择的行数据，取每条数据对应的ID
            var checkStatus = table.checkStatus('LAY-table')
                , checkData = checkStatus.data; //得到选中的数据
            if (checkData.length === 0) {
                return commonJs.openMsg('warning', '请选择要删除的数据！');
            }
            var ids = new Array();
            $.each(checkData, function (i, row) {
                ids[i] = row['id'];
            });

            commonJs.openConfirm('确定删除吗？', function () {
                commonJs.openAjax(prefix + "/batchRemove", {"ids": ids}, function (result) {
                    if (result.code == 0) {
                        commonJs.openMsg('success', '删除成功！', function () {
                            renderTable(); //数据刷新
                        });
                    } else {
                        commonJs.openMsg('error', result.msg);
                    }
                });
            });
        },
        add: function () {
            commonJs.openDialog('添加', prefix + '/add/0', {}, [], function (index, layero) {
                var iframeWindow = window['layui-layer-iframe' + index]
                    , submitID = 'LAY-operate-submit'
                    , submit = layero.find('iframe').contents().find('#' + submitID);

                //监听提交
                iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                    commonJs.openAjax(prefix + "/save", data, function (result) {
                        if (result.code == 0) {
                            commonJs.openMsg('success', '添加成功！', function () {
                                renderTable();             //数据刷新
                                layer.close(index);        //关闭弹层
                            });
                        } else {
                            commonJs.openMsg('error', result.msg);
                        }
                    });
                });
                submit.trigger('click');
            });
        }
    };

    $('.layui-btn.layuiadmin-btn-operate').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    // 渲染表格
    var renderTable = function () {
        commonJs.openLoad();
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'parentId',
            treeDefaultClose: true,
            treeLinkage: false,
            elem: '#LAY-table',
            url: prefix + '/list',
            page: false,
            cols: [[
                {type: 'numbers', width: 80},
                {field: 'name', title: '名称'},
                {field: 'icon', title: '图标', width: 80, templet: "#iconTpl"},
                {field: 'type', title: '类型', width: 80, templet: "#typeTpl"},
                {field: 'url', title: '地址'},
                {field: 'perms', title: '权限标识'},
                {toolbar: '#table-btn-operate', title: '操作'}
            ]],
            done: function () {
                commonJs.closeLoad();
            }
        });
    };
    renderTable();

    //初始化layui的table的tool操作
    table.on("tool(LAY-table)", function (e) {
        if ("del" === e.event) {
            commonJs.openConfirm('确定删除吗？', function () {
                commonJs.openAjax(prefix + "/remove", {"id": e.data.id}, function (result) {
                    if (result.code == 0) {
                        commonJs.openMsg('success', '删除成功！', function () {
                            renderTable();         //数据刷新
                        });
                    } else {
                        commonJs.openMsg('error', result.msg);
                    }
                });
            });
        } else if ("edit" === e.event) {
            commonJs.openDialog('编辑', prefix + '/edit/' + e.data.id, {}, [], function (index, layero) {
                var iframeWindow = window['layui-layer-iframe' + index]
                    , submitID = 'LAY-operate-submit'
                    , submit = layero.find('iframe').contents().find('#' + submitID);

                //监听提交
                iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                    commonJs.openAjax(prefix + "/update", data, function (result) {
                        if (result.code == 0) {
                            commonJs.openMsg('success', '编辑成功！', function () {
                                renderTable();       //数据刷新
                                layer.close(index);  //关闭弹层
                            });
                        } else {
                            commonJs.openMsg('error', result.msg);
                        }
                    });
                });
                submit.trigger('click');
            });
        } else if ("add" === e.event) {
            commonJs.openDialog('添加', prefix + '/add/' + e.data.id, {}, [], function (index, layero) {
                var iframeWindow = window['layui-layer-iframe' + index]
                    , submitID = 'LAY-operate-submit'
                    , submit = layero.find('iframe').contents().find('#' + submitID);

                //监听提交
                iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                    commonJs.openAjax(prefix + "/save", data, function (result) {
                        if (result.code == 0) {
                            commonJs.openMsg('success', '添加成功！', function () {
                                renderTable();       //数据刷新
                                layer.close(index);  //关闭弹层
                            });
                        } else {
                            commonJs.openMsg('error', result.msg);
                        }
                    });
                });
                submit.trigger('click');
            });
        }
    });

});

